package com.hbnu.net;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

/**
 * @author kittychen
 * @since 2025-10-28 17:12
 */
public class TcpClientDemo02 {
    public static void main(String[] args) {
        Socket socket = null;
        try {
            socket = new Socket("127.0.0.1", 8888);

            OutputStream outputStream = socket.getOutputStream();
            InputStream inputStream = socket.getInputStream();

            outputStream.write("地瓜地瓜，我是土豆，收到请回复，over".getBytes());

            // 接收服务端返回的消息
            byte[] bytes = new byte[1024];
            int len = inputStream.read(bytes);
            System.out.println("服务器返回的消息：" + new String(bytes, 0, len));
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (socket != null) {
                    socket.close();
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
